package com.pucong.gitee.database.backup.business.unRelational.redis.controller;

import com.pucong.gitee.database.backup.common.entity.database.AutoDataSource;
import com.pucong.gitee.database.backup.common.entity.database.dto.AutoDataSourceSaveDto;
import com.pucong.gitee.database.backup.common.entity.database.dto.AutoDataSourceTestConnectDto;
import com.pucong.gitee.database.backup.common.entity.redis.RedisConnect;
import com.pucong.gitee.database.backup.common.entity.redis.vo.AutoRedisInfoVo;
import com.pucong.gitee.database.backup.utils.Res;
import com.pucong.gitee.database.backup.business.unRelational.redis.service.AutoRedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author pucong
 * @url https://gitee.com/pucong
 * @create 2022-07-28
 */
@RestController
@RequestMapping("/auto/redis/crud")
@Api(value = "redis-API ", tags = "redis API")
public class AutoRedisController {

    @Autowired
    private AutoRedisService autoRedisService;

    @GetMapping("/getSourceInfoById/{id}")
    @ApiOperation(value = "根据主键查询", notes = "根据主键查询")
    public Res<AutoRedisInfoVo> getBaseInfoById(@ApiParam("主键") @PathVariable("id") String id) {
        AutoRedisInfoVo autoRedisInfoVo = autoRedisService.getBaseInfoById(id);
        return Res.ok(autoRedisInfoVo);
    }

    @PostMapping("/saveSourceInfo")
    @ApiOperation(value = "新增和修改数据库信息")
    public Res<Integer> addBaseInfo(@Validated @RequestBody RedisConnect dto) {
        int i = autoRedisService.saveBaseInfo(dto);
        return Res.ok(i);
    }

    @DeleteMapping("/deleteSourceInfo/{id}")
    @ApiOperation(value = "新增和修改数据库信息")
    public Res deleteSourceInfo(@ApiParam("主键") @PathVariable("id") String id) {
        autoRedisService.deleteSourceInfo(id);
        return Res.ok();
    }

    @PostMapping("/connectTest")
    @ApiOperation(value = "连接测试")
    public Res<Boolean> connectTest(@Validated @RequestBody RedisConnect redisConnect) {
        Boolean i = autoRedisService.connectTest(redisConnect);
        return Res.ok(i);
    }

}
